package com.ostrich.lib_common.net;

import java.lang.annotation.Annotation;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import androidx.lifecycle.LiveData;
import io.reactivex.Completable;
import retrofit2.CallAdapter;
import retrofit2.Retrofit;

/**
 * @Author : yaotianxue
 * @Time : On 2023/10/6 08:11
 * @Description : LiveDataCallAdapterFactory
 */
public class LiveDataCallAdapterFactory  extends CallAdapter.Factory{
    @Override
    public CallAdapter<?, ?> get(Type returnType, Annotation[] annotations, Retrofit retrofit) {

        Class<?> rawType = getRawType(returnType);
        if (rawType != LiveData.class) {
            throw new IllegalArgumentException("must be livedata");
        }

        Type observableType = getParameterUpperBound(0, (ParameterizedType) returnType);
        Class<?> rawObservableType = getRawType(observableType);

        if(rawObservableType != BaseResponseEntity.class){
            throw new IllegalArgumentException("ParameterizedType must be BaseResponseEntity");
        }


        return new LiveDataCallAdapter<>(observableType);
    }

    public static LiveDataCallAdapterFactory create(){
        return new LiveDataCallAdapterFactory();
    }
}
